package com.icebergteam.warfacewidget.rank.ui;

import java.util.ArrayList;
import org.jsoup.Jsoup;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.ListViewFactory;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.data.sql.RankTable;
import com.icebergteam.warfacewidget.rank.data.Top100ClansItem;
import com.icebergteam.warfacewidget.rank.data.Top100ClansParser;
import com.icebergteam.warfacewidget.R;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.RemoteViews;

public class Top100ClansListViewFactory extends ListViewFactory {
	public static String className = Top100ClansListViewFactory.class
			.getCanonicalName();
	
	private ArrayList<Top100ClansItem> listTop100ClansItems = new ArrayList<Top100ClansItem>();

	public Top100ClansListViewFactory(Context ctxt, Intent intent) {
		super(ctxt, intent);
		try {
				
			Top100ClansParser parser = new Top100ClansParser();
			
			//FIXME: url - id for request top100
			Cursor cursor = AppManager.getDatabase().getRankTable().getRankByUrl("http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=3&category=3&subcategory=1");
				
			while(cursor!= null && cursor.moveToNext()){
				listTop100ClansItems = (ArrayList<Top100ClansItem>) parser.parseXML(Jsoup.parse(cursor.getString(cursor.getColumnIndex(RankTable.FIELD_HTML.trim()))), AppManager.getTop100Settings().getResultItemsCount());
				
			}
			count = listTop100ClansItems.size();
			
			if(listTop100ClansItems.size() > AppManager.getTop100Settings().getResultItemsCount()){
				count = AppManager.getTop100Settings().getResultItemsCount();
			}
			
			cursor.close();
			LogManager.LogV(Top100ClansListViewFactory.className,LogManager.getExecuteMethodName(),
							((cursor == null) ? cursor == null
									: cursor.toString()),
							((ctxt == null) ? ctxt == null : ctxt.toString()), count);
		
		} catch (Exception e) {
			LogManager.LogEA(className, "bug", e.getLocalizedMessage());
			e.printStackTrace();
		} 

	}
	

	@Override
	public RemoteViews getViewAt(int position) {
		try {
			RemoteViews row = new RemoteViews(context.getPackageName(),
					R.layout.top100clans_list_item_widget_layout);
	
			LogManager.LogV(className, LogManager.getExecuteMethodName(), position, count);
			if(listTop100ClansItems != null && !listTop100ClansItems.isEmpty() && listTop100ClansItems.get(position) != null){
				row.setTextViewText(R.id.widget_rank_top100clans_list_item_clanname,
						listTop100ClansItems.get(position).getClanName());
				row.setTextViewText(R.id.widget_rank_top100clans_list_item_ownernickname,
						listTop100ClansItems.get(position).getOwnerNickname());
				
				row.setTextViewText(R.id.widget_rank_top100clans_list_item_pos,
							String.format("%s", listTop100ClansItems.get(position).getPos()));
					
				row.setTextViewText(R.id.widget_rank_top100clans_list_item_pos2,
						String.format("%s", listTop100ClansItems.get(position).getPos2()));
			
				row.setTextViewText(R.id.widget_rank_top100clans_list_item_pos3,
						String.format("%s", listTop100ClansItems.get(position).getPos3()));
				
//				Intent i = new Intent();
//				
//				i.putExtra(Top100ListViewFactory.POS_INTENT_KEY, (position));
//				row.setOnClickFillInIntent(R.id.LinearLayout1, i);
			
			
			}
			return row;
		} catch (NumberFormatException e) {
			LogManager.LogE(Top100ClansListViewFactory.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogManager.LogE(Top100ClansListViewFactory.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
			e.printStackTrace();
		}
		return null;
	}

}